//
//  CustomNavBar.m
//  ImGuider
//
//  Created by 陆涛 on 2018/10/30.
//  Copyright © 2018 imguider. All rights reserved.
//

#import "CustomNavBar.h"
#import "UIView+Gradient.h"

typedef NS_ENUM(NSUInteger, CustomNavBarTag) {
    
    CustomNavBarTagThemeImagView = 10086,
    CustomNavBarTagBlackbgView,
    CustomNavBarTagLineView
};

@interface CustomNavBar ()

@property (nonatomic, strong) UIView * barbgView;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation CustomNavBar

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        [self setupBroundview];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupBroundview];
    }
    return self;
}

- (void)setupBroundview {
    
    [[CustomNavBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [[CustomNavBar appearance] setShadowImage:[UIImage new]];
    
    UIView *barbgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavigationBarHeight)];
    barbgView.backgroundColor = [UIColor whiteColor];
    
    self.barbgView = barbgView;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = kBGColor;
    lineView.tag = CustomNavBarTagLineView;
    [barbgView addSubview:lineView];
    self.lineView = lineView;
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(barbgView.mas_left);
        make.right.equalTo(barbgView.mas_right);
        make.bottom.equalTo(barbgView.mas_bottom);
        make.height.mas_equalTo(1.0f);
    }];
    
    if (@available(iOS 10.0, *)) {} else {
        
        CGRect frame = self.barbgView.frame;
        frame.origin.y = -kStatusBarHeight;
        self.barbgView.frame = frame;
        
        [self addSubview:self.barbgView];
    }
}

- (void)didAddSubview:(UIView *)subview {
    
    [super didAddSubview:subview];
    
    
    if (@available(iOS 10.0.0, *)) {} else {
        
        if ([subview isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
            
            subview.alpha = 0;
        }
    }
    
    if ([subview isKindOfClass:NSClassFromString(@"_UIBarBackground")] ) {
        
        for (UIView *ssview in subview.subviews) {
            
            if ([ssview isKindOfClass:[UIVisualEffectView class]] || [ssview isKindOfClass:[UIImageView class]]) {
                
                ssview.alpha = 0;
                ssview.hidden = YES;
            }
        }
        
        [subview addSubview:self.barbgView];
    }
}

- (void)setBgColor:(UIColor *)bgColor {
    
    _bgColor = bgColor;
    
    self.barbgView.backgroundColor = [UIColor whiteColor];
}

- (void)setBarBGAlpha:(CGFloat)barBGAlpha {
    
    _barBGAlpha = barBGAlpha;
    
    self.barbgView.alpha = barBGAlpha;
}

- (void)setIsHiddenLineView:(BOOL)isHiddenLineView {
    
    _isHiddenLineView = isHiddenLineView;
    
    if (isHiddenLineView) {
        
        self.lineView.hidden = YES;
    }
}

- (void)setNavThemeGradient {
    
    if ([self.barbgView viewWithTag:CustomNavBarTagThemeImagView]) {
        return;
    }
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultBG"]];
    bgImageView.tag = CustomNavBarTagThemeImagView;
    [self.barbgView addSubview:bgImageView];
    
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.barbgView);
    }];
}

- (void)setNavBlackGradient {
    
    if ([self.barbgView viewWithTag:CustomNavBarTagBlackbgView]) {
        return;
    }
    
    self.barbgView.backgroundColor = UIColor.clearColor;
    self.barbgView.alpha = 1;
    self.isHiddenLineView = true;
    
    [self.barbgView gradientWithColorArray:@[[UIColor colorWithWhite:0 alpha:0.3], [UIColor colorWithWhite:0 alpha:0.01]] frame:CGRectMake(0, 0, kScreenWidth, kNavigationBarHeight) isLeftToRight:NO];
}
@end

